<?php
namespace app\ausadmin\controller;
use app\ausadmin\controller\Common;
use think\Request;
use think\Db;
use think\File;
use think\Session;
use think\Cookie;
use Godok\Org\FileManager;
class Order extends Common
{
	//查询充值订单	
	public function index(){
		$where = ' a.is_delete = 1';
		$pages = 20;//定义默认显示每页显示记录数
		$pageParam    = ['query' =>[]];//分页带参数
		if(input('order_no')!=''){
			$where .= ' AND a.order_no = '.input('order_no');
			//var_dump(input('order_no'));die;
			$pageParam['query']['order_no'] = input('order_no');
		}
		if(input('name')!=''){
			$where .= ' AND b.name LIKE '."'%".input('name')."%' ";
			$pageParam['query']['name'] = input('name');
		}
		$b = 'operator';
		$c = 'version';
		$list = Db::name('operator_order')
			 ->alias('a')
			 ->join("$b b",'a.oid = b.id')
			 ->join("$c c",'a.version = c.id')
			 ->field('a.*,b.name,c.name as vname,c.describes,c.free,c.vision,c.smapp_template_id')
			 ->where($where)
			 ->where('a.pay_status',1)
			 ->order('a.create_time desc')
			 ->paginate($pages,false, $pageParam);
			// var_dump($list);die;
		$this->assign('list',$list);
		$oid = 0;$m = 'ausadmin'; $c = 'Order'; $action="index"; $uid=session('adminid'); $querystring = '查看开户订单';
                $ip = get_ip();$os = get_os();
                set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
		return $this->fetch();
	}

	public function renew($id=''){
		if ( Request::instance()->isPost()) {
			
			$dq = (int)input('dq');
			$time = (int)input('endtime');
			if($time){
				$maturity =  strtotime("+".$time."month",$dq);
			}else{
				return ['code'=>-1050, 'msg'=>'请选择到期时长!'];
			}
			$data['payable_amount'] = $time * input('payable_amount');
 			$data['real_amount'] = $time * input('price');
			$data['order_no'] = '20' . date('ymdHis') . rand(1000, 9999);
			$data['pay_status'] = 2;
			$data['create_user'] = session("adminid");
			$data['create_time'] = time();
			$data['is_delete'] = 1;
			$data['new_expiration_date'] = $maturity;
			$data['oid'] = (int)input('oid');
			$data['version'] = (int)input('version');
			$data['old_expiration_date'] = input('dq');
			if(Db::name('operator_order')->insert($data)){
				return ['code'=>0, 'msg'=>'续费成功！'];
 			}else{
 				return ['code'=>-1050, 'msg'=>'续费失败！'];
 			}
		}
		$id = Request::instance()->get('id','','number_int');
		if($id){
			$ret = Db::name('operator_order')->where('id',$id)->find();
			$this->assign('v',$ret);
			$list = Db::name('version')->where('is_delete',1)->select();
			$this->assign('list',$list);
			$ver = Db::name('version')->where('id',$ret['version'])->find();
			$this->assign('ver',$ver);
			return view();
		}else{
			die('非正常访问!');
		}

	}

}